package com.xxx.auth.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xxx.auth.entity.enums.PermissionType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Entity
@Comment("权限表")
@Table(name = "t_permission")
@EntityListeners(AuditingEntityListener.class)
public class Permission extends TimeEntity implements Serializable {

    public static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "CustomUUID")
    @GenericGenerator(name = "CustomUUID", strategy = "com.xxx.auth.common.generator.CustomUUIDGenerator")
    @Column(name = "id", unique = true)
    private String id;

    @Column(nullable = false)
    @Comment("权限名称")
    @Schema(title = "权限名称")
    private String name;

    @Column(nullable = false)
    @Comment("描述")
    @Schema(title = "描述")
    private String description;

    @Column
    @Comment("权限类型：PROJECT-用于项目的权限 ORGANIZATION-用于组织管理的权限 APPLICATION-用于应用管理的权限 SYSTEM-用于系统管理的权限")
    @Schema(title = "权限类型：PROJECT-用于项目的权限 ORGANIZATION-用于组织管理的权限 APPLICATION-用于应用管理的权限 SYSTEM-用于系统管理的权限")
    @Enumerated(EnumType.STRING)
    private PermissionType permissionType;

    @ManyToMany(mappedBy = "permissionSet", fetch = FetchType.LAZY)
    @JsonIgnore
    private Set<Application> applicationSet = new HashSet<>();

    @ManyToOne
    @JoinColumn(name = "permission_category_id", referencedColumnName = "id")
    @JsonIgnore
    private PermissionCategory permissionCategory;

}
